package com.ydc.ln.commons.utils.page;

import java.util.List;

/**
 * 分布工具类
 * @author ydc
 * @date 上午10:39:30 2013-5-12 
 * @param <T>
 */
public class Pagination<T> {
	public static final int DEFAULT_PAGENO = 1;
	public static final int DEFAULT_PAGESIZE = 10;
	public static final int DEFAULT_INDEXCOUNT = 10;
	// 以下2个属性必须指定
	private List<T> records; // 分页数据
	private int totalRecord; // 总记录数

	private int pageNo = DEFAULT_PAGENO; // 当前页码,第几页
	private int pageSize = DEFAULT_PAGESIZE; // 每页显示的记录数,每页显示多少条数据

	// 这三项根据已知数据计算
	private int totalPage; // 总页数
	private int startIndex; // 开始索引
	private int endIndex; // 结束索引

	private int indexCount = DEFAULT_INDEXCOUNT;// 显示的索引数目,如:10的话， 则显示1-10， 2-11

	// public Pagination() {}

	public Pagination(List<T> records, int totalRecord) {
		this(records, totalRecord, DEFAULT_PAGENO, DEFAULT_PAGESIZE);
	}

	public Pagination(List<T> records, int totalRecord, int pageNo, int pageSize) {
		this.records = records;
		this.totalRecord = totalRecord;
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		init();
	}

	public static int getRightPageSize(int pageSize, int totalRecord) {
		pageSize = pageSize < 1 ? Pagination.DEFAULT_PAGESIZE : (pageSize > totalRecord ? Pagination.DEFAULT_PAGESIZE : pageSize);
		return pageSize;
	}

	/**
	 * 验证pageNo,获取正确的pageNo
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param totalRecord
	 * @return
	 */
	public static int getRightPageNo(int pageNo, int pageSize, int totalRecord) {
		// 根据总记录数和每页显示数计算总页数(totalRecord+pageSize->totalPage)
		int totalPage = totalRecord / pageSize;
		totalPage = (totalRecord % pageSize == 0) ? totalPage : (totalPage + 1);

		// 判断pageNo
		pageNo = pageNo <= 1 ? 1 : (pageNo >= totalPage ? totalPage : pageNo);
		return pageNo;
	}

	// 数据初始化
	private void init() {
		// 根据总记录数和每页显示数计算总页数(totalRecord+pageSize->totalPage)
		totalPage = this.totalRecord / this.pageSize;
		totalPage = (this.totalRecord % pageSize == 0) ? totalPage : (totalPage + 1);

		// 判断pageNo
		pageNo = pageNo <= 1 ? 1 : (pageNo >= getTotalPage() ? getTotalPage() : pageNo);

		// 根据索引数目，当前页，总页数计算开始索引和结束索引(indexCount+pageNo+totalPage->startIndex+endIndex)
		startIndex = indexCount / 2;
		startIndex = pageNo - (indexCount % 2 == 0 ? (startIndex - 1) : startIndex);
		endIndex = pageNo + indexCount / 2;
		// 1 <= startIndex < pageNo < endIndex <= totalPage
		// startIndex = pageNo - indexCount/2
		// endIndex = pageNo + indexCount/2
		if (startIndex < 1) {
			startIndex = 1;
			if (totalPage >= indexCount) {
				endIndex = indexCount;
			} else {
				endIndex = totalPage;
			}
		}
		if (endIndex > totalPage) {
			endIndex = totalPage;
			if (endIndex > indexCount) {
				startIndex = endIndex - indexCount + 1;
			} else {
				startIndex = 1;
			}
		}
	}

	/**
	 * 获取分页数据
	 * 
	 * @return
	 */
	public List<T> getRecords() {
		return records;
	}

	/**
	 * 获取总记录数
	 * 
	 * @return
	 */
	public int getTotalRecord() {
		return totalRecord;
	}

	/**
	 * 当前页数(第几页)
	 * 
	 * @return
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public int getPrePageNo() {
		int prePageNo = (pageNo <= 1) ? 1 : (pageNo - 1);
		return prePageNo;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPageNo() {
		int nextPageNo = (pageNo >= getTotalPage()) ? getTotalPage() : (pageNo + 1);
		return nextPageNo;
	}

	/**
	 * 每页显示数据记录数
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 起始索引
	 * 
	 * @return
	 */
	public int getStartIndex() {
		return startIndex;
	}

	/**
	 * 结束索引
	 * 
	 * @return
	 */
	public int getEndIndex() {
		return endIndex;
	}

	public int getIndexCount() {
		return indexCount;
	}

	public void setIndexCount(int indexCount) {
		this.indexCount = indexCount;
		// 修改后重新初始化
		init();
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		init();
	}
}
